﻿// Autore: Alessio Parma

using System;

namespace RecipesManager.Data.Implementation
{
    sealed partial class Event : IEvent
    {
        readonly IGuestParticipations _guests;
        readonly IRecipesServings _recipes;

        public Event()
        {
            _guests = new GuestParticipations(() => GuestParticipations, () => Id);
            _recipes = new RecipesServings(() => RecipesServings, () => Id);
        }

        #region IEvent Members

        DateTime? IEvent.Date
        {
            get { return Date; }
        }

        IGuestParticipations IEvent.Guests
        {
            get { return _guests; }
        }

        string IEvent.Name
        {
            get { return Name; }
        }

        IRecipesServings IEvent.Recipes
        {
            get { return _recipes; }
        }

        public bool Equals(IEvent other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Id == other.Id;
        }

        public void Close()
        {
            HasEnded = true;
            Manager.SaveChanges();
        }

        public void Update(string name, string notes, DateTime? date)
        {
            Name = name;
            Notes = notes;
            Date = date;
            Manager.SaveChanges();
        }

        #endregion

        public static Event New(string name, string notes, DateTime? date)
        {
            var ev = CreateEvent(0, name, notes);
            ev.Date = date;
            return ev;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            return ReferenceEquals(this, obj) || Equals(obj as IEvent);
        }

        public override int GetHashCode()
        {
            return Id;
        }

        public override string ToString()
        {
            return string.Format("Event(Name: {0}, Date: {1})", Name, Date);
        }
    }

    sealed class EventObjects : Objects<Event, IEvent>, IEvents
    {
        public EventObjects() : base(Manager.EventSet)
        {
        }

        #region IEvents Members

        public IEvent Add(string name, string notes, DateTime? date)
        {
            return Add(Event.New(name, notes, date));
        }

        #endregion
    }
}